package org.chandler.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.*;
import org.apache.http.HttpEntity;  
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;  
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;  
import org.apache.http.client.HttpClient;  
import org.apache.http.util.EntityUtils;


public class HttpUtil {
	@SuppressWarnings("deprecation")
	private static HttpClient httpClient = new DefaultHttpClient();  
	
	@SuppressWarnings("deprecation")
	public static String get(String url) {  
        String body = null;  
        try {  
            // Get请求  
            HttpGet httpget = new HttpGet(url);  
            httpget.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
            // 发送请求  
            HttpResponse httpresponse = httpClient.execute(httpget);  
            // 获取返回数据  
            HttpEntity entity = httpresponse.getEntity();  
            body = EntityUtils.toString(entity);  
            if (entity != null) {  
                entity.consumeContent();  
            }  
        } catch (ParseException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return body;  
    } 
	
	
	@SuppressWarnings("deprecation")
	public static String get(String url, List<NameValuePair> params) {  
        String body = null;  
        try {  
            // Get请求  
            HttpGet httpget = new HttpGet(url);  
            httpget.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0");
            // 发送请求  
            HttpResponse httpresponse = httpClient.execute(httpget);  
            // 获取返回数据  
            HttpEntity entity = httpresponse.getEntity();  
            body = EntityUtils.toString(entity);  
            if (entity != null) {  
                entity.consumeContent();  
            }  
        } catch (ParseException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }
        return body;  
    }  
}
